{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "91cb31a0-41b2-4760-9297-b619f8b47736",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "# store_asset"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d89ae40-6a75-4d6d-91f5-ced21c1f94e9",
   "metadata": {},
   "source": [
    "Stores an asset (file) to S3 compliant COS (Cloud Object Store) to provide it to subsequent stages to avoid re-processing of data.\n",
    "\n",
    "WARNING: currently only minio supported"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a28bc37-3944-4e5c-b8a0-e66be078ae4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install wget==3.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "stainless-purpose",
   "metadata": {},
   "outputs": [],
   "source": [
    "import wget\n",
    "wget.download(\n",
    "    'https://raw.githubusercontent.com/IBM/claimed/master/component-library/claimed_utils.py'\n",
    ")\n",
    "from claimed_utils import parse_args_to_parameters\n",
    "from io import BytesIO\n",
    "from minio import Minio\n",
    "import os\n",
    "import os.path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tired-access",
   "metadata": {},
   "outputs": [],
   "source": [
    "# checkpoint_ip minio endpoint\n",
    "checkpoint_ip = os.environ.get('checkpoint_ip')\n",
    "\n",
    "# checkpoint_user minio user\n",
    "checkpoint_user = os.environ.get('checkpoint_user', 'minio')\n",
    "\n",
    "# checkpoint_pass minio pw\n",
    "checkpoint_pass = os.environ.get('checkpoint_pass', 'minio123')\n",
    "\n",
    "# checkpoint_bucket minio bucket\n",
    "checkpoint_bucket = os.environ.get('checkpoint_bucket', 'checkpoint')\n",
    "\n",
    "# name of asset to pull\n",
    "asset_name = os.environ.get('asset_name')\n",
    "\n",
    "parse_args_to_parameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bound-tuning",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "client = Minio(checkpoint_ip, checkpoint_user, checkpoint_pass, secure=False)\n",
    "\n",
    "size = os.path.getsize(asset_name)\n",
    "with open(asset_name, 'rb') as fh:\n",
    "    buf = BytesIO(fh.read())\n",
    "    result = client.put_object(\n",
    "        checkpoint_bucket, asset_name, buf, length=size\n",
    "    )"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
